package com.ice.project.datamining.model;

import java.util.Random;

import org.apache.log4j.Logger;

import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

import com.ice.project.datamining.gui.MainWindow;

public class CreditProbability {

	/** Main window **/
	private MainWindow mainWindow ;
	/** Log **/
	private static Logger LOG = Logger.getLogger(CreditProbability.class);
	private NaiveBayes nb;
	private Instances instancesModel;
	private Instances instancesTest;
	private double[][] confusionMatrix;
	
	public double[][] getConfusionMatrix() {
		return confusionMatrix;
	}

	public CreditProbability(MainWindow window){
		try {
			mainWindow = window;
			// Retrieving source files
			DataSource modelData = new DataSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("data/training_credit_g_7.arff"));
			instancesModel = modelData.getDataSet();
			instancesModel.setClassIndex(instancesModel.numAttributes() - 1);
			
			DataSource testData = new DataSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("data/test_credit_g_7.arff"));
			instancesTest = testData.getDataSet();
			instancesTest.setClassIndex(instancesTest.numAttributes()-1);
			
			// Building Bayesien classifier
			nb = new NaiveBayes();
			nb.buildClassifier(instancesModel);
			
			// Confusion matrix
			Evaluation evalTest = new Evaluation(instancesTest);
			evalTest.crossValidateModel(nb, instancesTest, 10, new Random(1));
			confusionMatrix = evalTest.confusionMatrix();
		} catch (Exception ex) {
			LOG.error(ex);
			ex.printStackTrace();
		}
	}
	
	public void computDataSet()	{
		try {
		// Building instance
			String[] data = new String[7];
			data[0] = mainWindow.getFormPanel().getJcCheckingStatus().getSelectedItem().toString();
			data[1] = mainWindow.getFormPanel().getJcCreditHistory().getSelectedItem().toString();
			data[2] = mainWindow.getFormPanel().getJcPurpose().getSelectedItem().toString(); 
			data[3] = mainWindow.getFormPanel().getJcSavingStatus().getSelectedItem().toString(); 
			data[4] = mainWindow.getFormPanel().getJcEmployment().getSelectedItem().toString(); 
			data[5] = mainWindow.getFormPanel().getJcPropertyMagnitude().getSelectedItem().toString(); 
			data[6] = mainWindow.getFormPanel().getJcHousing().getSelectedItem().toString(); 

			Instance instance = new Instance(8);
			instance.setDataset(instancesModel);
			instance.setValue(instancesModel.attribute("checking_status"), (String)data[0]);
			instance.setValue(instancesModel.attribute("credit_history"), (String)data[1]);
			instance.setValue(instancesModel.attribute("purpose"), (String)data[2]);
			instance.setValue(instancesModel.attribute("savings_status"), (String)data[3]);
			instance.setValue(instancesModel.attribute("employment"), (String)data[4]);
			instance.setValue(instancesModel.attribute("property_magnitude"), (String)data[5]);	
			instance.setValue(instancesModel.attribute("housing"), (String)data[6]);
			
			double[] distribution  = nb.distributionForInstance(instance);
			System.out.println(distribution[0]+"---"+distribution[1]);		
			
			mainWindow.refreshMeterPlot(distribution[0]*100);
		} catch (Exception ex) {
			LOG.error(ex);
			ex.printStackTrace();
		}
	}
	
	public double getPrecision() {
		double precision = 0D;
		double sumTotal = 0D;
		double sumGood = 0D;
		if (this.confusionMatrix != null) {
			for (int l = 0; l < confusionMatrix.length; l++) {
				for (int c = 0; c < confusionMatrix[l].length; c++) {
					if (l == c) {
						sumGood += confusionMatrix[l][c];
					}
					sumTotal += confusionMatrix[l][c];
				}
			}
		}
		
		if (sumTotal != 0) {
			precision = sumGood / sumTotal;
		}
		
		return precision;
	}
}
